home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 319 / compsrc1 / explow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  15.4 KB  |  551 lines

  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "tree.h"
  25. #include "flags.h"
  26. #include "expr.h"
  27.  
  28. /* Return an rtx for the sum of X and the integer C.  */
  29.  
  30. rtx
  31. plus_constant (x, c)
  32.      register rtx x;
  33.      register int c;
  34. {
  35.   register RTX_CODE code = GET_CODE (x);
  36.   register enum machine_mode mode = GET_MODE (x);
  37.   int all_constant = 0;
  38.  
  39.   if (c == 0)
  40.     return x;
  41.  
  42.   if (code == CONST_INT)
  43.     return gen_rtx (CONST_INT, VOIDmode, (INTVAL (x) + c));
  44.  
  45.   /* If adding to something entirely constant, set a flag
  46.      so that we can add a CONST around the result.  */
  47.   if (code == CONST)
  48.     {
  49.       x = XEXP (x, 0);
  50.       all_constant = 1;
  51.     }
  52.   else if (code == SYMBOL_REF || code == LABEL_REF)
  53.     all_constant = 1;
  54.  
  55.   /* The interesting case is adding the integer to a sum.
  56.      Look for constant term in the sum and combine
  57.      with C.  For an integer constant term, we make a combined
  58.      integer.  For a constant term that is not an explicit integer,
  59.      we cannot really combine, but group them together anyway.  */
  60.  
  61.   if (GET_CODE (x) == PLUS)
  62.     {
  63.       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  64.     {
  65.       c += INTVAL (XEXP (x, 0));
  66.       x = XEXP (x, 1);
  67.     }
  68.       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  69.     {
  70.       c += INTVAL (XEXP (x, 1));
  71.       x = XEXP (x, 0);
  72.     }
  73.       else if (CONSTANT_P (XEXP (x, 0)))
  74.     {
  75.       return gen_rtx (PLUS, mode,
  76.               plus_constant (XEXP (x, 0), c),
  77.               XEXP (x, 1));
  78.     }
  79.       else if (CONSTANT_P (XEXP (x, 1)))
  80.     {
  81.       return gen_rtx (PLUS, mode,
  82.               XEXP (x, 0),
  83.               plus_constant (XEXP (x, 1), c));
  84.     }
  85. #ifdef OLD_INDEXING
  86.       /* Detect adding a constant to an indexed address
  87.      of the form (PLUS (MULT (REG) (CONST)) regs-and-constants).
  88.      Keep the (MULT ...) at the top level of addition so that
  89.      the result is still suitable for indexing and constants
  90.      are combined.  */
  91.       else if (GET_CODE (XEXP (x, 0)) == MULT)
  92.     {
  93.       return gen_rtx (PLUS, mode, XEXP (x, 0),
  94.               plus_constant (XEXP (x, 1), c));
  95.     }
  96.       else if (GET_CODE (XEXP (x, 1)) == MULT)
  97.     {
  98.       return gen_rtx (PLUS, mode, plus_constant (XEXP (x, 0), c),
  99.               XEXP (x, 1));
  100.     }
  101. #endif
  102.     }
  103.   if (c != 0)
  104.     x = gen_rtx (PLUS, mode, x, gen_rtx (CONST_INT, VOIDmode, c));
  105.  
  106.   if (all_constant)
  107.     return gen_rtx (CONST, mode, x);
  108.   else
  109.     return x;
  110. }
  111.  
  112. /* If X is a sum, return a new sum like X but lacking any constant terms.
  113.    Add all the removed constant terms into *CONSTPTR.
  114.    X itself is not altered.  The result != X if and only if
  115.    it is not isomorphic to X.  */
  116.  
  117. rtx
  118. eliminate_constant_term (x, constptr)
  119.      rtx x;
  120.      int *constptr;
  121. {
  122.   int c;
  123.   register rtx x0, x1;
  124.  
  125.   if (GET_CODE (x) != PLUS)
  126.     return x;
  127.  
  128.   /* First handle constants appearing at this level explicitly.  */
  129.   if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  130.     {
  131.       *constptr += INTVAL (XEXP (x, 0));
  132.       return eliminate_constant_term (XEXP (x, 1), constptr);
  133.     }
  134.  
  135.   if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  136.     {
  137.       *constptr += INTVAL (XEXP (x, 1));
  138.       return eliminate_constant_term (XEXP (x, 0), constptr);
  139.     }
  140.  
  141.   c = 0;
  142.   x0 = eliminate_constant_term (XEXP (x, 0), &c);
  143.   x1 = eliminate_constant_term (XEXP (x, 1), &c);
  144.   if (x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  145.     {
  146.       *constptr += c;
  147.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  148.     }
  149.   return x;
  150. }
  151.  
  152. /* Return an rtx for the size in bytes of the value of EXP.  */
  153.  
  154. rtx
  155. expr_size (exp)
  156.      tree exp;
  157. {
  158.   return expand_expr (size_in_bytes (TREE_TYPE (exp)), 0, SImode, 0);
  159. }
  160.  
  161. /* Not yet really written since C does not need it.  */
  162.  
  163. rtx
  164. lookup_static_chain ()
  165. {
  166.   abort ();
  167. }
  168.  
  169. /* Return a copy of X in which all memory references
  170.    and all constants that involve symbol refs
  171.    have been replaced with new temporary registers.
  172.    Also emit code to load the memory locations and constants
  173.    into those registers.
  174.  
  175.    If X contains no such constants or memory references,
  176.    X itself (not a copy) is returned.
  177.  
  178.    X may contain no arithmetic except addition, subtraction and multiplication.
  179.    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
  180.  
  181. static rtx
  182. break_out_memory_refs (x)
  183.      register rtx x;
  184. {
  185.   if (GET_CODE (x) == MEM || GET_CODE (x) == CONST
  186.       || GET_CODE (x) == SYMBOL_REF)
  187.     {
  188.       register rtx temp = force_reg (Pmode, x);
  189.       mark_reg_pointer (temp);
  190.       x = temp;
  191.     }
  192.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  193.        || GET_CODE (x) == MULT)
  194.     {
  195.       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  196.       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  197.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  198.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  199.     }
  200.   return x;
  201. }
  202.  
  203. /* Given a memory address or facsimile X, construct a new address,
  204.    currently equivalent, that is stable: future stores won't change it.
  205.  
  206.    X must be composed of constants, register and memory references
  207.    combined with addition, subtraction and multiplication:
  208.    in other words, just what you can get from expand_expr if sum_ok is 1.
  209.  
  210.    Works by making copies of all regs and memory locations used
  211.    by X and combining them the same way X does.
  212.    You could also stabilize the reference to this address
  213.    by copying the address to a register with copy_to_reg;
  214.    but then you wouldn't get indexed addressing in the reference.  */
  215.  
  216. rtx
  217. copy_all_regs (x)
  218.      register rtx x;
  219. {
  220.   if (GET_CODE (x) == REG)
  221.     {
  222.       if (REGNO (x) != FRAME_POINTER_REGNUM)
  223.     x = copy_to_reg (x);
  224.     }
  225.   else if (GET_CODE (x) == MEM)
  226.     x = copy_to_reg (x);
  227.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  228.        || GET_CODE (x) == MULT)
  229.     {
  230.       register rtx op0 = copy_all_regs (XEXP (x, 0));
  231.       register rtx op1 = copy_all_regs (XEXP (x, 1));
  232.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  233.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  234.     }
  235.   return x;
  236. }
  237.  
  238. /* Return something equivalent to X but valid as a memory address
  239.    for something of mode MODE.  When X is not itself valid, this
  240.    works by copying X or subexpressions of it into registers.  */
  241.  
  242. rtx
  243. memory_address (mode, x)
  244.      enum machine_mode mode;
  245.      register rtx x;
  246. {
  247.   register rtx tem, oldx;
  248.  
  249.   /* By passing constant addresses thru registers
  250.      we get a chance to cse them.  */
  251.   if (! cse_not_expected && CONSTANT_P (x))
  252.     return force_reg (Pmode, x);
  253.  
  254.   /* Accept a QUEUED that refers to a REG
  255.      even though that isn't a valid address.
  256.      On attempting to put this in an insn we will call protect_from_queue
  257.      which will turn it into a REG, which is valid.  */
  258.   if (GET_CODE (x) == QUEUED
  259.       && GET_CODE (QUEUED_VAR (x)) == REG)
  260.     return x;
  261.  
  262.   /* We get better cse by rejecting indirect addressing at this stage.
  263.      Let the combiner create indirect addresses where appropriate.
  264.      For now, generate the code so that the subexpressions useful to share
  265.      are visible.  But not if cse won't be done!  */
  266.   oldx = x;
  267.   if (! cse_not_expected && GET_CODE (x) != REG)
  268.     x = break_out_memory_refs (x);
  269.  
  270.   /* At this point, any valid address is accepted.  */
  271.   GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  272.  
  273.   /* If it was valid before but breaking out memory refs invalidated it,
  274.      use it the old way.  */
  275.   if (memory_address_p (mode, oldx))
  276.     goto win2;
  277.  
  278.   /* Perform machine-dependent transformations on X
  279.      in certain cases.  This is not necessary since the code
  280.      below can handle all possible cases, but machine-dependent
  281.      transformations can make better code.  */
  282.   LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  283.  
  284.   /* PLUS and MULT can appear in special ways
  285.      as the result of attempts to make an address usable for indexing.
  286.      Usually they are dealt with by calling force_operand, below.
  287.      But a sum containing constant terms is special
  288.      if removing them makes the sum a valid address:
  289.      then we generate that address in a register
  290.      and index off of it.  We do this because it often makes
  291.      shorter code, and because the addresses thus generated
  292.      in registers often become common subexpressions.  */
  293.   if (GET_CODE (x) == PLUS)
  294.     {
  295.       int constant_term = 0;
  296.       rtx y = eliminate_constant_term (x, &constant_term);
  297.       if (constant_term == 0
  298.       || ! memory_address_p (mode, y))
  299.     return force_operand (x, 0);
  300.  
  301.       y = plus_constant (copy_to_reg (y), constant_term);
  302.       if (! memory_address_p (mode, y))
  303.     return force_operand (x, 0);
  304.       return y;
  305.     }
  306.   if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  307.     return force_operand (x, 0);
  308.  
  309.   /* Last resort: copy the value to a register, since
  310.      the register is a valid address.  */
  311.   return force_reg (Pmode, x);
  312.  
  313.  win2:
  314.   x = oldx;
  315.  win:
  316.   if (flag_force_addr && optimize && GET_CODE (x) != REG
  317.       /* Don't copy an addr via a reg if it is one of our stack slots.
  318.      If we did, it would cause invalid REG_EQUIV notes for parms.  */
  319.       && ! (GET_CODE (x) == PLUS
  320.         && (XEXP (x, 0) == frame_pointer_rtx
  321.         || XEXP (x, 0) == arg_pointer_rtx)))
  322.     return force_reg (Pmode, x);
  323.   return x;
  324. }
  325.  
  326. /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
  327.  
  328. rtx
  329. memory_address_noforce (mode, x)
  330.      enum machine_mode mode;
  331.      rtx x;
  332. {
  333.   int ambient_force_addr = flag_force_addr;
  334.   rtx val;
  335.  
  336.   flag_force_addr = 0;
  337.   val = memory_address (mode, x);
  338.   flag_force_addr = ambient_force_addr;
  339.   return val;
  340. }
  341.  
  342. /* Return a modified copy of X with its memory address copied
  343.    into a temporary register to protect it from side effects.
  344.    If X is not a MEM, it is returned unchanged (and not copied).
  345.    Perhaps even if it is a MEM, if there is no need to change it.  */
  346.  
  347. rtx
  348. stabilize (x)
  349.      rtx x;
  350. {
  351.   register rtx addr;
  352.   if (GET_CODE (x) != MEM)
  353.     return x;
  354.   addr = XEXP (x, 0);
  355.   if (rtx_unstable_p (addr))
  356.     {
  357.       rtx temp = copy_all_regs (addr);
  358.       rtx mem;
  359.       if (GET_CODE (temp) != REG)
  360.     temp = copy_to_reg (temp);
  361.       mem = gen_rtx (MEM, GET_MODE (x), temp);
  362.       /* Mark returned memref with in_struct
  363.      if it's in an array or structure. */
  364.       if (GET_CODE (addr) == PLUS || x->in_struct)
  365.     mem->in_struct = 1;
  366.       return mem;
  367.     }
  368.   return x;
  369. }
  370.  
  371. /* Copy the value or contents of X to a new temp reg and return that reg.  */
  372.  
  373. rtx
  374. copy_to_reg (x)
  375.      rtx x;
  376. {
  377.   register rtx temp = gen_reg_rtx (GET_MODE (x));
  378.   emit_move_insn (temp, x);
  379.   return temp;
  380. }
  381.  
  382. /* Like copy_to_reg but always give the new register mode Pmode
  383.    in case X is a constant.  */
  384.  
  385. rtx
  386. copy_addr_to_reg (x)
  387.      rtx x;
  388. {
  389.   register rtx temp = gen_reg_rtx (Pmode);
  390.   emit_move_insn (temp, x);
  391.   return temp;
  392. }
  393.  
  394. /* Like copy_to_reg but always give the new register mode MODE
  395.    in case X is a constant.  */
  396.  
  397. rtx
  398. copy_to_mode_reg (mode, x)
  399.      enum machine_mode mode;
  400.      rtx x;
  401. {
  402.   register rtx temp = gen_reg_rtx (mode);
  403.   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
  404.     abort ();
  405.   emit_move_insn (temp, x);
  406.   return temp;
  407. }
  408.  
  409. /* Load X into a register if it is not already one.
  410.    Use mode MODE for the register.
  411.    X should be valid for mode MODE, but it may be a constant which
  412.    is valid for all integer modes; that's why caller must specify MODE.
  413.  
  414.    The caller must not alter the value in the register we return,
  415.    since we mark it as a "constant" register.  */
  416.  
  417. rtx
  418. force_reg (mode, x)
  419.      enum machine_mode mode;
  420.      rtx x;
  421. {
  422.   register rtx temp, insn;
  423.  
  424.   if (GET_CODE (x) == REG)
  425.     return x;
  426.   temp = gen_reg_rtx (mode);
  427.   insn = emit_move_insn (temp, x);
  428.   /* Let optimizers know that TEMP's value never changes
  429.      and that X can be substituted for it.  */
  430.   if (CONSTANT_P (x))
  431.     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUIV, x, 0);
  432.   return temp;
  433. }
  434.  
  435. /* If X is a memory ref, copy its contents to a new temp reg and return
  436.    that reg.  Otherwise, return X.  */
  437.  
  438. rtx
  439. force_not_mem (x)
  440.      rtx x;
  441. {
  442.   register rtx temp;
  443.   if (GET_CODE (x) != MEM)
  444.     return x;
  445.   temp = gen_reg_rtx (GET_MODE (x));
  446.   emit_move_insn (temp, x);
  447.   return temp;
  448. }
  449.  
  450. /* Copy X to TARGET (if it's nonzero and a reg)
  451.    or to a new temp reg and return that reg.  */
  452.  
  453. rtx
  454. copy_to_suggested_reg (x, target)
  455.      rtx x, target;
  456. {
  457.   register rtx temp;
  458.   if (target && GET_CODE (target) == REG)
  459.     temp = target;
  460.   else
  461.     temp = gen_reg_rtx (GET_MODE (x));
  462.   emit_move_insn (temp, x);
  463.   return temp;
  464. }
  465.  
  466. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  467.    This pops when ADJUST is positive.  ADJUST need not be constant.  */
  468.  
  469. void
  470. adjust_stack (adjust)
  471.      rtx adjust;
  472. {
  473.   adjust = protect_from_queue (adjust, 0);
  474.  
  475. #ifdef STACK_GROWS_DOWNWARD
  476.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  477. #else
  478.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  479. #endif
  480. }
  481.  
  482. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  483.    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
  484.  
  485. void
  486. anti_adjust_stack (adjust)
  487.      rtx adjust;
  488. {
  489.   adjust = protect_from_queue (adjust, 0);
  490.  
  491. #ifdef STACK_GROWS_DOWNWARD
  492.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  493. #else
  494.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  495. #endif
  496. }
  497.  
  498. /* Round the size of a block to be pushed up to the boundary required
  499.    by this machine.  SIZE is the desired size, which need not be constant.  */
  500.  
  501. rtx
  502. round_push (size)
  503.      rtx size;
  504. {
  505. #ifdef STACK_BOUNDARY
  506.   int align = STACK_BOUNDARY / BITS_PER_UNIT;
  507.   if (align == 1)
  508.     ;
  509.   if (GET_CODE (size) == CONST_INT)
  510.     {
  511.       int new = (INTVAL (size) + align - 1) / align * align;
  512.       if (INTVAL (size) != new)
  513.     size = gen_rtx (CONST_INT, VOIDmode, new);
  514.     }
  515.   else
  516.     {
  517.       size = expand_divmod (0, CEIL_DIV_EXPR, Pmode, size,
  518.                 gen_rtx (CONST_INT, VOIDmode, align),
  519.                 0, 1);
  520.       size = expand_mult (Pmode, size,
  521.               gen_rtx (CONST_INT, VOIDmode, align),
  522.               0, 1);
  523.     }
  524. #endif /* STACK_BOUNDARY */
  525.   return size;
  526. }
  527.  
  528. /* Return an rtx representing the register or memory location
  529.    in which a scalar value of data type VALTYPE
  530.    was returned by a function call to function FUNC.
  531.    FUNC is a FUNCTION_DECL node if the precise function is known,
  532.    otherwise 0.  */
  533.  
  534. rtx
  535. hard_function_value (valtype, func)
  536.      tree valtype;
  537.      tree func;
  538. {
  539.   return FUNCTION_VALUE (valtype, func);
  540. }
  541.  
  542. /* Return an rtx representing the register or memory location
  543.    in which a scalar value of mode MODE was returned by a library call.  */
  544.  
  545. rtx
  546. hard_libcall_value (mode)
  547.      enum machine_mode mode;
  548. {
  549.   return LIBCALL_VALUE (mode);
  550. }
  551.